var currentPage = 1;
var timeStatus = 1; //默认1表示进入抢购
var type = 1; //默认1表示服装
//列表
loadMyEssay(timeStatus, type);

function loadMyEssay(timeStatus, type) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#rushGoodsList").html("");
		$.ajax({
			url: urlcore + "/api/rushGoods/selectRushGoodsList1?timeStatus=" + timeStatus + "&type=" + type + "&size=" + 30 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			async: false,
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {

						var goodsTitle = n.goods.goodsTitle == null ? "" : n.goods.goodsTitle;
						var cheapPrice = n.cheapPrice == null ? "" : n.cheapPrice;
						var tradePrice = n.goods.tradePrice == null ? "" : n.goods.tradePrice;
						var tradeNumber = n.goods.tradeNumber == null ? 0 : n.goods.tradeNumber;
						//var saleNum=n.goods.saleNum==null?0:n.goods.saleNum;
						var saleNum = n.surplusstock == null ? 0 : n.surplusstock;

						var rushAmount = n.rushAmount == null ? 0 : n.rushAmount;
						
						var coverUrl=n.goods.coverUrl==null?'http://app.jiagangwangluo.com/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.goods.coverUrl;
						if(timeStatus == 1) {
							//var bar1 = n.goods.saleNum / n.rushAmount * 100;
							var bar1 = saleNum / n.rushAmount * 100;
							var thislist =
								'<dl class="qianggou_goods">' +
								'<dt>' +
								'<img src="' + coverUrl + '" />' +
								'</dt>' +
								'<dd>' +
								'<h5>' + goodsTitle + '</h5>' +
								'<h6>&yen;' + cheapPrice + '<span>&yen;' + tradePrice + '</span></h6>' +
								'<div class="clearfix">' +
								'<div class="fl">' +
								'<div class="qianggou_xinxi">' +
								'<span>' + tradeNumber + '件起批</span>' +
								'<span>已售出' + saleNum + '件</span>' +
								'</div>' +
								'<div class="progress1">' +
								'<div class="progress-bar1" style="width: ' + bar1 + '%;">' +

								'</div>' +
								'<div class="progress-value1">' + saleNum + '/' + rushAmount + '</div>' +
								'</div>' +
								'</div>' +
								'<a href="2.2.0goods_detail.html?id=' + n.goods.id + '&goodsType=' + 3 + '&packRate=' + cheapPrice + '&breadcrumbType=4" class="fr qianggou_a">立即抢购</a>' +
								'</div>' +
								'</dd>' +
								'</dl>';
							$('#rushGoodsList').append(thislist);
						} else if(timeStatus == 0) {

							var thislist2 =
								'<dl class="qianggou_goods">' +
								'<dt>' +
								'<img class="rushGoodsImg" src="' + coverUrl + '" />' +
								'</dt>' +
								'<dd class="goodsId1">' +
								'<h5>' + goodsTitle + '</h5>' +
								'<h6 class="yugao_price">&yen;' + cheapPrice + '<span>&yen;' + tradePrice + '</span></h6>' +
								'<div class="qianggou_xinxi">' +
								'<span>' + tradeNumber + '件起批</span>' +
								'</div>' +
								'<em class="yugao_em remind" style="cursor:pointer;">设置提醒</em>' +
								'<input type="hidden" id="goodsId" class="goodsId" value="' + n.goods.id + '"/>' +
								'</dd>' +
								'</dl>';
							$('#rushGoodsList').append(thislist2);

						}

					});

					$(function() {
						$('.remind').click(function() {
							var goodsId = $(this).parent('dd[class*=goodsId1]').find('input[class*=goodsId]').val();
							$.ajax({
								url: urlcore + "/api/rushGoods/pushRushGoodsMsg?goodsId=" + goodsId,
								type: "get",
								dataType: 'json',
								contentType: "application/json;charset=utf-8",
								success: function(data) {
									if(data.success == true) {
										layer.alert("设置成功！");
									} else if(data.code == 'OVERTIME') {
										var thisUrl = window.location.href;
										//alert(thisUrl);
										if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
											top.window.location.href = "1.0.0denglu.html";
										}

									} else {
										if(data.msg != '空数据') {
											layer.alert(data.msg)
										} else {
											$('#thiscount').text(0);
										}
									}
								}
							});
						})
						
						
						//固定宽高
						var imgList1 = document.getElementsByClassName("rushGoodsImg");  
					    var w=290;
					    var h=290;
					    for(var i=0;i<imgList1.length;i++){
					        if(imgList1[i].width>w || imgList1[i].height>h){  
					            imgList1[i].width = w;  
					            imgList1[i].heigth = h;  
					        }  
					    }  

					})
					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}

function selectRushGoods(timeStatus) {
	if(timeStatus == 1) {
		$('.crazyRush').css('display', 'block');
		$('.setRemind').css('display', 'none');
	} else {
		$('.crazyRush').css('display', 'none');
		$('.setRemind').css('display', 'block');
	}
	sessionStorage.setItem("timeStatus", timeStatus);
	loadMyEssay(timeStatus, '');
}

function selectRushGoodsByType(type) {
	var ss = sessionStorage.getItem("timeStatus") == null ? 1 : sessionStorage.getItem("timeStatus");
	loadMyEssay(ss, type);
}

//倒计时
$(function() {
	countDown("2018/01/15 24:00:00", null, "#demo02 .hour", "#demo02 .minute", "#demo02 .second");
});

function countDown(time, day_elem, hour_elem, minute_elem, second_elem) {
	//if(typeof end_time == "string")
	var end_time = new Date(time).getTime(), //月份是实际月份-1
		//current_time = new Date().getTime(),
		sys_second = (end_time - new Date().getTime()) / 1000;
	var timer = setInterval(function() {
		if(sys_second > 0) {
			sys_second -= 1;
			var day = Math.floor((sys_second / 3600) / 24);
			var hour = Math.floor((sys_second / 3600) % 24);
			var minute = Math.floor((sys_second / 60) % 60);
			var second = Math.floor(sys_second % 60);
			day_elem && $(day_elem).text(day); //计算天
			$(hour_elem).text(hour < 10 ? "0" + hour : hour); //计算小时
			$(minute_elem).text(minute < 10 ? "0" + minute : minute); //计算分
			$(second_elem).text(second < 10 ? "0" + second : second); // 计算秒
		} else {
			clearInterval(timer);
		}
	}, 1000);
}